package com.pri.chat.activity;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Outline;
import android.os.Bundle;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.netease.nim.uikit.net.SharedHelper;
import com.netease.nim.uikit.net.StatusBarUtil;
import com.netease.nim.uikit.net.entity.BannerBean;
import com.netease.nim.uikit.net.entity.BannerModel;
import com.netease.nim.uikit.net.entity.BaseBean;
import com.netease.nim.uikit.net.entity.MemberBean;
import com.netease.nim.uikit.net.rxjava.HttpMethods;
import com.netease.nim.uikit.net.subscribers.ProgressSubscriber;
import com.netease.nim.uikit.net.subscribers.SubscriberOnNextListener;
import com.pri.chat.R;
import com.pri.chat.adapter.MemberJggAdapter;
import com.pri.chat.base.BaseActivity;
import com.pri.chat.event.EventTag;
import com.pri.chat.event.MessageEvent;
import com.pri.chat.model.DTModel;
import com.pri.chat.utils.Des3Util;
import com.pri.chat.utils.GlideImageLoader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.vondear.rxtool.RxActivityTool;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 推荐互动页面
 */
public class RecommendedInteractiveActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.srl_all)
    SmartRefreshLayout srlAll;
    @BindView(R.id.rv)
    RecyclerView recyclerView;



    private List<MemberBean> mList;

    private MemberJggAdapter mAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_recommended_interactive;
    }

    @Override
    protected void initView() {
        rl_center.setBackgroundColor(getResources().getColor(R.color.red));
        mTitle.setText("推荐互动");
        mTitle.setTextColor(getResources().getColor(R.color.white));
        back.setImageResource(R.mipmap.ic_back_white);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.red), 0);
        StatusBarUtil.setDarkMode(this);

        initRecyclerView();
        initSrl();
    }

    private void initBanner() {
        banner.setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 30);
            }
        });

        banner.setClipToOutline(true);

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(img_list);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.Default);
        //设置标题集合（当banner样式有显示title时）
//        banner.setBannerTitles(titles);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(1500);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    private void initRecyclerView() {
        mList = new ArrayList<>();
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        mAdapter = new MemberJggAdapter(R.layout.item_member_jgg, mList);
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id",mAdapter.getData().get(position).getId());
                RxActivityTool.skipActivity(mContext, OtherPeopleMsgActivity.class,bundle);
            }
        });
    }

    public void refresh() {
        isShowDialog = false;
        mList.clear();
        mAdapter.notifyDataSetChanged();
        getTuiJianList();
    }

    private boolean isShowDialog = true;
    private void initSrl() {
        srlAll.setOnRefreshListener(refresh_layout -> {
            refresh_layout.finishRefresh(1000);
            refresh();
        });
        srlAll.setOnLoadmoreListener(refresh_layout -> {
            refresh_layout.finishLoadmore(1000);
            isShowDialog = false;
            getTuiJianList();
        });
    }

    private void getTuiJianList() {
        SubscriberOnNextListener<BaseBean<List<MemberBean>>> onNextListener = baseBean -> {
            mList.addAll(baseBean.getData());
            mAdapter.notifyDataSetChanged();
        };
        Map<String,Object> params = new HashMap<>();
        params.put("heartTime",mList.size() == 0?0:mList.get(mList.size()-1).getHeartTime());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(params));
        HttpMethods.getInstance().getTuiJianListPage(new ProgressSubscriber<>(onNextListener, mContext, isShowDialog),requestBody);
    }

    @Override
    protected void initData() {
        httpGetBanner();
        getTuiJianList();
    }

    private List<String> img_list = new ArrayList<>();
    private List<BannerBean> lunbolistBeanList = new ArrayList<>();
    private void httpGetBanner() {
        SubscriberOnNextListener<BaseBean<List<BannerBean>>> onNextListener = baseBean -> {
            if (img_list.isEmpty()) {
                lunbolistBeanList.addAll(baseBean.getData());
                for (int i = 0; i < lunbolistBeanList.size(); i++) {
                    img_list.add(lunbolistBeanList.get(i).getBannerPath());
                }
                initBanner();
            }
        };
        BannerModel evaListModel = new BannerModel("yyjjBanner");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), Des3Util.getJiaMi(evaListModel));
        HttpMethods.getInstance().getBannerList(new ProgressSubscriber<>(onNextListener, mContext, false), requestBody);
    }

}